package service

import (
	"errors"
	"my-desktop/dao"
	"my-desktop/models"
	"time"
)

func Register(user models.SysUser) (data *models.SysUser, err error) {
	user.Avatar = "http://codehzp.cn/images/avatar.jpg"
	user.LoginDate = time.Now()
	user.CreateTime = time.Now()
	user.UpdateTime = time.Now()

	user1, _ := dao.GetUserByUserName(user.UserName)
	if user1 != nil {
		return nil, errors.New("用户已经存在")
	}
	return dao.Register(user)
}

func Login(username, password string) (data *models.SysUser, err error) {
	return dao.Login(username, password)
}

func GetUserByUserID(userID uint64) (data *models.UserVO, err error) {
	user, err := dao.GetUserByUserID(userID)
	if err != nil {
		return nil, err
	}
	data = &models.UserVO{}
	data.UserID = user.UserID
	data.UserName = user.UserName
	data.NickName = user.NickName
	data.Email = user.Email
	data.PhoneNumber = user.PhoneNumber
	data.CreateTime = user.CreateTime
	data.Avatar = user.Avatar
	data.Sex = user.Sex
	return data, nil
}
